package com.sunseagear.wind.modules.sys.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sunseagear.common.base.http.R;
import com.sunseagear.common.base.http.RPage;
import com.sunseagear.common.base.mvc.controller.BaseBeanController;
import com.sunseagear.wind.common.aspectj.annotation.Log;
import com.sunseagear.wind.common.aspectj.enums.LogType;
import com.sunseagear.wind.modules.sys.entity.Dict;
import com.sunseagear.wind.modules.sys.entity.query.DictQuery;
import com.sunseagear.wind.modules.sys.service.IDictService;
import com.sunseagear.wind.common.utils.DictUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 消息模版控制器
 */
@RestController
@RequestMapping("/sys/dict")
@Log(title = "字典管理")
@RequiredArgsConstructor
public class DictController extends BaseBeanController {

    private final IDictService dictService;

    /**
     * 字典
     */
    @GetMapping(value = "")
    @SaCheckLogin
    public R<Map<String, List<DictUtils.Dict>>> get() {
        try {
            //放入数据字典
            return R.success(DictUtils.getDict());
        } catch (Exception e) {
            log.error("字典获取失败", e);
            return R.fail("获取失败");
        }
    }

    /**
     * 根据页码和每页记录数，以及查询条件动态加载数据
     */
    @GetMapping(value = "list")
    @Log(logType = LogType.SELECT)
    @SaCheckPermission("sys:dict:list")
    public RPage<Dict> list(DictQuery query) {
        //加入条件
        LambdaQueryWrapper<Dict> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByAsc(Dict::getSort);
        queryWrapper.eq(Dict::getGid, query.getGid());
        queryWrapper.nested(ObjectUtil.isNotEmpty(query.getKeyword()), i -> i.like(Dict::getLabel, query.getKeyword()).or().like(Dict::getValue, query.getKeyword()));

        // 预处理
        Page<Dict> pageBean = dictService.page(getPage(), queryWrapper);
        return RPage.success(pageBean);
    }

    /**
     * 添加
     * @param entity 实体对象
     * @return 操作结果
     */
    @PostMapping("add")
    @Log(logType = LogType.INSERT)
    @SaCheckPermission("sys:dict:add")
    public R<Void> add(@Validated @RequestBody Dict entity) {
        dictService.save(entity);
        DictUtils.initDict();
        return R.success("添加成功");
    }

    /**
     * 修改
     * @param entity 实体对象
     * @return 操作结果
     */
    @PutMapping("update")
    @Log(logType = LogType.UPDATE)
    @SaCheckPermission("sys:dict:add")
    public R<Void> update(@Validated @RequestBody Dict entity) {
        dictService.saveOrUpdate(entity);
        DictUtils.initDict();
        return R.success("更新成功");
    }

    /**
     * 删除
     * @param idList 主键
     * @return 操作结果
     */
    @DeleteMapping("delete")
    @Log(logType = LogType.DELETE)
    @SaCheckPermission("sys:dict:delete")
    public R<Void> delete(@RequestBody List<Long> idList) {
        dictService.removeBatchByIds(idList);
        DictUtils.initDict();
        return R.success("删除成功");
    }

}
